require 'statistic/fields_counter'
require "rexml/document"

module Statistic
  
  class XMLFieldsCounter < FieldsCounter
    
    def initialize( crawl_table, xpaths = ["./*"], conditions = "status = 200" )
      super crawl_table, conditions
      @xpaths = xpaths
    end
    
    def analyse(c)
      begin
        doc = REXML::Document.new c.body
      rescue REXML::ParseException => e  
        #continue with a partial xml parsed document if possible
        return false if doc.nil?   
      end
      
      nodes = REXML::XPath.match doc, @xpath
      @xpaths.each { |xpath|
        nodes = REXML::XPath.match doc, xpath
        nodes.each { |n| @fields_stat[n.name] += 1 }
      }
      true
    end
  end
  
end